package com.runnor.common.websys.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runnor.common.websys.system.entity.ApiResult;
import com.runnor.common.websys.system.entity.SysRole;
import com.runnor.common.websys.system.entity.bo.SysRoleBo;
import com.runnor.common.websys.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("api/sysrole")
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('roles:list')")
    public ResponseEntity page(@RequestParam(defaultValue = "1") Integer current,
                               @RequestParam(defaultValue = "10") Integer size,
                               SysRoleBo sysRole) {

        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StringUtils.hasText(sysRole.getName()), "name", sysRole.getName())
                .like(StringUtils.hasText(sysRole.getNotes()), "notes", sysRole.getNotes())
                .eq(sysRole.getEnable() != null, "enable", sysRole.getEnable())
                .eq("deleted", 0)
                .like(StringUtils.hasText(sysRole.getMenuNo()), "menu_no", sysRole.getMenuNo());

        queryWrapper.orderByDesc("created");

        Page data = sysRoleService.pages(new Page(current, size), queryWrapper);

        return ResponseEntity.ok(ApiResult.ok(data));
    }

    /**
     * 根据id查询角色
     */
    @GetMapping("/getById")
    public ResponseEntity getById(String no) {
        SysRole sysRole = sysRoleService.getById(no);

        /**
         * 查询当前角色 拥有的 菜单
         */
        List<String> roles = sysRoleService.selectRoleSysDict(no);
        if (roles != null) {
            String[] array = new String[roles.size()];
            roles.toArray(array);

            sysRole.setRoles(array);
        }

        return ResponseEntity.ok(ApiResult.ok(sysRole));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('roles:add')")
    public ResponseEntity save(SysRole sysRole) {
        boolean save = sysRoleService.save(sysRole);

        if (sysRole.getRoles().length > 0) {
            for (String role : sysRole.getRoles()) {
                /**
                 * 角色分配菜单
                 */
                sysRoleService.InsertIntoRoleSysDict(sysRole.getNo(), role);
            }
        }

        return ResponseEntity.ok(ApiResult.ok(save));
    }

    /**
     * 修改
     */
    @PostMapping("/updateById")
    @PreAuthorize("hasAuthority('roles:edit')")
    public ResponseEntity updateById(SysRole sysRole) {
        boolean save = sysRoleService.updateById(sysRole);

        /**
         * 删除 角色拥有的菜单
         */
        sysRoleService.deleteRoleSysDict(sysRole.getNo());

        if (sysRole.getRoles().length > 0) {
            for (String role : sysRole.getRoles()) {
                /**
                 * 角色分配菜单
                 */
                sysRoleService.InsertIntoRoleSysDict(sysRole.getNo(), role);
            }
        }

        return ResponseEntity.ok(ApiResult.ok(save));
    }

    /**
     * 删除
     */
    @GetMapping("/deletes")
    @PreAuthorize("hasAuthority('roles:del')")
    public ResponseEntity deletes(String nos) {
        boolean b = sysRoleService.removeBatchByIds(Arrays.asList(nos.split(",")));

        return ResponseEntity.ok(ApiResult.ok(b));
    }

    /**
     * 删除
     */
    @GetMapping("/removeById")
    @PreAuthorize("hasAuthority('roles:del')")
    public ResponseEntity removeById(String no) {
        boolean b = sysRoleService.removeById(no);

        return ResponseEntity.ok(ApiResult.ok(b));
    }

    @GetMapping("/selectOneByName")
    public ResponseEntity selectOneByName(String name, String no) {

        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq(StringUtils.hasText(name), "name", name);

        queryWrapper.ne(StringUtils.hasText(no), "no", no);

        SysRole sysRole = sysRoleService.getOne(queryWrapper);

        return ResponseEntity.ok(ApiResult.ok(sysRole));
    }

    @GetMapping("/list")
    public ResponseEntity list() {

        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("enable", 1);

        queryWrapper.eq("deleted", 0);

        List list = sysRoleService.list(queryWrapper);

        return ResponseEntity.ok(ApiResult.ok(list));
    }

    /**
     * 根据 no 修改 状态
     */
    @GetMapping("/updateEnableByNo")
    @PreAuthorize("hasAuthority('roles:edit')")
    public ResponseEntity updateEnableByNo(String no, Boolean enable) {

        UpdateWrapper updateWrapper = new UpdateWrapper();

        updateWrapper.set("enable", enable);
        updateWrapper.eq("no", no);

        boolean update = sysRoleService.update(null, updateWrapper);

        return ResponseEntity.ok(ApiResult.ok(update));
    }
}
